package com.tongda.core.repository;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tongda.core.utils.DBHelper;
import com.tongda.core.utils.DishUtil;
import com.tongda.core.utils.MenuDBHelper;
import com.tongda.core.vo.Dish;
import com.tongda.core.vo.DishesPic;
import com.tongda.core.vo.DishesSetDishes;
import com.tongda.core.vo.DishesSetDishesReplace;
import com.tongda.core.vo.SpecialDishe;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * 菜肴dao 用于维护本地菜谱数据
 * 
 * @author Administrator
 */
public class MenuDishDao {
	private String TABLE_NAME = "dish";
	private MenuDBHelper dbHelper;
	private SQLiteDatabase db;
	private static MenuDishDao instance;
	private String[] selectionArgs;
	private String ORDER_TABLE_NAME = "order_dish";

	public MenuDishDao(Context context) {
		this.dbHelper = MenuDBHelper.getInstance(context);
	}

	public static synchronized MenuDishDao getInstance(Context context) {
		if (instance == null) {
			instance = new MenuDishDao(context);
		}
		return instance;
	}

	/**
	 * 新增菜谱
	 * 
	 * @param dish
	 */
	public void insert(Dish dish, DishUtil dish_util, String tableName) {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("id", dish.getDishesId());
		values.put("categoryId", dish.getCategoryId());
		values.put("price", dish.getPrice() + "");
		values.put("name", dish.getDishesName());
		values.put("dishescode", dish.getDishesCode());
		values.put("salenum", dish.getSaleNum());
		values.put("unitname", dish.getUnitName());
		values.put("isset", dish.getIsSet());
		values.put("isRecommend", dish.getIsRecommend());
		values.put("recommendDesc", dish.getRecommendDesc());
		values.put("isSpecialty", dish.getIsSpecialty());
		values.put("specialtyDesc", dish.getSpecialtyDesc());
		values.put("memberPrice", dish.getMemberPrice() + "");
		values.put("specialPrice", dish.getSpecialPrice() + "");
		// values.put("spiName", dish.getSpiName());
		// values.put("spTime",dish.getSpTime());
		// values.put("spDateStr",dish.getSpDateStr());
		List<SpecialDishe> specialDishes = dish.getSpecialDishes();
		String specialDishesStr = dish_util.spDishes2Str(specialDishes);
		values.put("spiStr", specialDishesStr);
		// 将套餐内的菜肴资源以及可替换菜肴写入数据库
		List<DishesSetDishes> dsDishes = dish.getDsDishes();
		String dsDishesArrayStr = dish_util.dsDishes2JsonStr(dsDishes);
		values.put("dsDishes", dsDishesArrayStr);

		// 把从网络获取的图片资源取其中的地址重组成jsonarr写入数据库中
		List<DishesPic> pics = dish.getDisImgs();
		String picStr = dish_util.pics2JsonStr(pics);
		values.put("url", picStr);
		String lowPicStr = dish_util.lowPics2JsonStr(pics);
		values.put("lowurl", lowPicStr);

		values.put("dishesTasteStr", dish.getDishesTasteStr());
		values.put("pungentLevel", dish.getPungentLevel());
		values.put("isStopSell", dish.getIsStopSell());
		values.put("isRulingPrice", dish.getIsRulingPrice());
		if (dish.getIsRulingPrice() != null
				&& dish.getIsRulingPrice().equals("1")) {
			values.put("rulingPrice", dish.getPrice() + "");
		}
		values.put("dishesStyleStr", dish.getDishesStyleStr());
		values.put("dishesMaterialStr", dish.getDishesMaterialStr());
		values.put("notes", dish.getNotes());
		values.put("estimate", dish.getEstimate());
		values.put("dishorderNum", 0);
		db.insert(tableName, null, values);
		// 后加
		dbHelper.closeDatabase();
	}

	/**
	 * 清空当前菜谱
	 */
	public void clearDish() {
		try {
			db = dbHelper.openDatabase();
			String checkSql = "SELECT name FROM sqlite_master WHERE type='table' and name='dish'";
			Cursor cursor = db.rawQuery(checkSql, null);
			if (cursor.getCount() > 0) {
				// 调试阶段暂时都清空 因为表结构经常变化
				db.execSQL("drop table dish");
				db.execSQL(MenuDBHelper.DISH_TABLE_CREATE);
			} else {
				db.execSQL(MenuDBHelper.DISH_TABLE_CREATE);
			}
			String sql = "delete from dish;";
			db.execSQL(sql);
			// 后加
			dbHelper.closeDatabase();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static Dish buildSearchCode(Dish dish) {
		StringBuffer buff = new StringBuffer();
		String dishesCode = dish.getDishesCode();
		char[] arr = dishesCode.toCharArray();
		for (int i = 0; i < arr.length; i++) {
			char c = arr[i];
			String str = String.valueOf(c).toUpperCase();
			// buff.append(DishService.codeMap.get(str));
		}
		return dish;
	}

	/**
	 * 根据数字编码查询菜肴
	 * 
	 * @param searchCode
	 * @return
	 */
	public List<Dish> searchBySearchCodes(String searchCode) {
		List<Dish> dishes = null;
		StringBuffer buff = new StringBuffer();
		// buff.append("select * from dish");
		buff.append("select * from dish where 1=1");
		if (!"".equals(searchCode)) {
		}
		buff.append(" and searchcode like ");
		buff.append("'%" + searchCode + "%'");

		buff.append(" order by salenum desc");
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery(buff.toString(), null);
		if (cursor.getCount() > 0) {
			dishes = new ArrayList<Dish>();
			while (cursor.moveToNext()) {
				String id = cursor.getString(cursor.getColumnIndex("id"));
				String name = cursor.getString(cursor.getColumnIndex("name"));
				String dishescode = cursor.getString(cursor
						.getColumnIndex("dishescode"));
				String searchcode = cursor.getString(cursor
						.getColumnIndex("searchcode"));
				String salenum = cursor.getString(cursor
						.getColumnIndex("salenum"));
				String unitname = cursor.getString(cursor
						.getColumnIndex("unitname"));
				Dish dish = new Dish();
				dish.setDishesId(id);
				dish.setDishesName(name);
				dish.setPrice(new BigDecimal(cursor.getString(cursor
						.getColumnIndex("price"))));
				dish.setDishesCode(dishescode);
				if (salenum != null && !salenum.equals("")) {
					dish.setSaleNum(Integer.valueOf(salenum).intValue());
				} else {
					dish.setSaleNum(0);
				}
				dish.setUnitName(unitname);
				dish.setSaleNum(1);
				dishes.add(dish);
			}
		}
		// 后加
		dbHelper.closeDatabase();
		return dishes;
	}

	/**
	 * 获取所有相同菜类id的菜肴（参数为空则获取所有菜肴）
	 * 
	 * @param params
	 */
	public List<Dish> getDishesByParams(Map<String, String> params) {
		List<Dish> dishList = new ArrayList<Dish>();
		String selection = " 1 = 1 ";
		List<String> selectionArgsList = null;
		if (params != null) {
			selectionArgsList = new ArrayList<String>();
			Set<String> keys = params.keySet();
			for (String key : keys) {
				Object value = params.get(key);
				String operator = "=";
				if (value != null) {
					String[] valueArray = value.toString().split("_");
					if (valueArray.length == 1) {
						selectionArgsList.add(value.toString());
					} else if (valueArray.length == 2) {
						operator = valueArray[0];
						selectionArgsList.add(valueArray[1] + "");
					}
				}
				selection += " and " + key + "  " + operator + "? ";
			}
			selectionArgs = selectionArgsList.toArray(new String[] {});
		}

		try {
			db = dbHelper.openDatabase();
			String groupBy = null;
			String having = null;
			String orderBy = null;
			String limit = null;
			if (params != null) {
				Cursor cursor = db.query(TABLE_NAME, null, selection,
						selectionArgs, groupBy, having, orderBy, limit);
				dishList = getCursorDishes(cursor);
			} else {
				Cursor cursor = db.query(TABLE_NAME, null, null, null, null,
						null, null);
				dishList = getCursorDishes(cursor);
			}
		} catch (Exception e) {
			Log.e(this.toString(), e.getMessage());
		}
		dbHelper.closeDatabase();
		return dishList;
	}

	/**
	 * 获取招牌，推荐，特价，会员特价菜肴
	 * 
	 * @param which
	 *            （数据库dish表的列名）
	 */
	public List<Dish> get4Dishes(String which) {
		List<Dish> dishes = null;
		try {
		db = dbHelper.openDatabase();
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where "
				+ which + " !='null'", null);
		 dishes = getCursorDishes(cursor);
		cursor.close();
		} catch (Exception e) {
			dishes = new ArrayList<Dish>();
		}
		dbHelper.closeDatabase();
		return dishes;
	}

	/**
	 * 根据菜名找到这道菜在表中的菜类id
	 * 
	 * @param which
	 *            （数据库dish表的列名）
	 */
	public String getdcId(String dishName) {
		String dcIdStr = "";
		db = dbHelper.openDatabase();
		if (dishName != null) {
			Cursor cursor = db.rawQuery("select * from " + TABLE_NAME
					+ " where name = ?", new String[] { dishName });
			if (cursor.moveToNext()) {
				dcIdStr = cursor.getString(cursor.getColumnIndex("categoryId"));
			}
			cursor.close();
		}
		dbHelper.closeDatabase();
		return dcIdStr;
	}

	/**
	 * 获取符合cursor条件的所有菜肴
	 * 
	 * @param cursor
	 */
	public static List<Dish> getCursorDishes(Cursor cursor) {
		List<Dish> dishes = new ArrayList<Dish>();
		while (cursor.moveToNext()) {
			Dish dish = new Dish();
			String id = cursor.getString(cursor.getColumnIndex("id"));
			String categoryId = cursor.getString(cursor
					.getColumnIndex("categoryId"));
			BigDecimal price = new BigDecimal(cursor.getString(cursor
					.getColumnIndex("price")));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String dishescode = cursor.getString(cursor
					.getColumnIndex("dishescode"));
			float salenum = Float.valueOf(cursor.getString(cursor
					.getColumnIndex("salenum")));
			String unitname = cursor.getString(cursor
					.getColumnIndex("unitname"));
			String isset = cursor.getString(cursor.getColumnIndex("isset"));
			String isRecommend = cursor.getString(cursor
					.getColumnIndex("isRecommend"));
			String recommendDesc = cursor.getString(cursor
					.getColumnIndex("recommendDesc"));
			String isSpecialty = cursor.getString(cursor
					.getColumnIndex("isSpecialty"));
			String specialtyDesc = cursor.getString(cursor
					.getColumnIndex("specialtyDesc"));
			String memberPriceStr = cursor.getString(cursor
					.getColumnIndex("memberPrice"));

			BigDecimal memberPrice = null;
			if (!memberPriceStr.equals("null")) {
				memberPrice = new BigDecimal(memberPriceStr);
			}
			String specialPriceStr = cursor.getString(cursor
					.getColumnIndex("specialPrice"));
			BigDecimal specialPrice = null;
			if (!specialPriceStr.equals("null")) {
				specialPrice = new BigDecimal(specialPriceStr);
			}
			/*
			 * 获取特价时段资源
			 */
			String spiJson = cursor.getString(cursor.getColumnIndex("spiStr"));
			List<SpecialDishe> spiDishes = new ArrayList<SpecialDishe>();
			JSONArray spiArrays;
			try {
				spiArrays = new JSONArray(spiJson);
				for (int i = 0; i < spiArrays.length(); i++) {
					JSONObject object = spiArrays.getJSONObject(i);
					SpecialDishe spiDishe = new SpecialDishe();
					spiDishe.setSpDateStr(object.getString("spDateStr"));
					spiDishe.setSpTime(object.getString("spTime"));
					String spiPriceStr = object.getString("specialPrice");
					BigDecimal spiPrice = null;
					if (!spiPriceStr.equals("null")) {
						spiPrice = new BigDecimal(spiPriceStr);
					}
					spiDishe.setSpecialPrice(spiPrice);
					spiDishe.setSpiName(object.getString("spiName"));

					spiDishes.add(spiDishe);
				}
			} catch (JSONException e1) {
				e1.printStackTrace();
			}

			/*
			 * 获取图片资源
			 */
			String urlJson = cursor.getString(cursor.getColumnIndex("url"));
			List<String> picUrls = new ArrayList<String>();
			JSONArray picArrays;
			try {
				picArrays = new JSONArray(urlJson);
				for (int i = 0; i < picArrays.length(); i++) {
					JSONObject object = picArrays.getJSONObject(i);
					picUrls.add(object.getString("picUrl"));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			/*
			 * 获取低分辨率图片资源
			 */
			String lowurlJson = cursor.getString(cursor
					.getColumnIndex("lowurl"));
			List<String> lowpicUrls = new ArrayList<String>();
			JSONArray lowpicArrays;
			try {
				lowpicArrays = new JSONArray(lowurlJson);
				for (int i = 0; i < lowpicArrays.length(); i++) {
					JSONObject object = lowpicArrays.getJSONObject(i);
					lowpicUrls.add(object.getString("piclowUrl"));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			/*
			 * 获取套餐资源
			 */
			String dsDishesStr = cursor.getString(cursor
					.getColumnIndex("dsDishes"));
			List<DishesSetDishes> dsDishes = new ArrayList<DishesSetDishes>();
			try {
				JSONArray dsDishesArray = new JSONArray(dsDishesStr);
				for (int i = 0; i < dsDishesArray.length(); i++) {
					DishesSetDishes dsDish = new DishesSetDishes();
					JSONObject dsDishObject = dsDishesArray.getJSONObject(i);
					dsDish.setDsDishesId(dsDishObject.getString("dsDishesId"));
					dsDish.setMr_dishesId(dsDishObject.getString("mr_dishesId"));
					dsDish.setMr_dishesName(dsDishObject
							.getString("mr_dishesName"));
					dsDish.setMr_dishesCode(dsDishObject
							.getString("mr_dishesCode"));
					dsDish.setMr_unitNum(dsDishObject.getString("mr_unitNum"));
					dsDish.setMr_unitName(dsDishObject.getString("mr_unitName"));

					dsDish.setDishesId(dsDishObject.getString("dishesId"));
					dsDish.setDishesName(dsDishObject.getString("dishesName"));
					dsDish.setDishesCode(dsDishObject.getString("dishesCode"));
					dsDish.setUnitNum(dsDishObject.getString("unitNum"));
					dsDish.setUnitName(dsDishObject.getString("unitName"));
					// 处理可替换菜肴
					List<DishesSetDishesReplace> dishesSetDishesReplaces = new ArrayList<DishesSetDishesReplace>();
					String dsReplaceStr = dsDishObject
							.getString("dishesSetDishesReplaces");
					ObjectMapper objectmapper = new ObjectMapper();
					DishesSetDishesReplace[] temp = objectmapper.readValue(
							dsReplaceStr, DishesSetDishesReplace[].class);
					dishesSetDishesReplaces = Arrays.asList(temp);
					/*
					 * 另外一种可用的更具体的方法 JSONArray dsReplacesArray = new
					 * JSONArray(dsReplaceStr); for(int
					 * j=0;j<dsReplacesArray.length();j++){
					 * DishesSetDishesReplace dsreplace = new
					 * DishesSetDishesReplace(); JSONObject dsreplaceObject =
					 * dsReplacesArray.getJSONObject(j);
					 * dsreplace.setDishesId(dsreplaceObject
					 * .getString("dishesId"));
					 * dsreplace.setDishesName(dsreplaceObject
					 * .getString("dishesName"));
					 * dsreplace.setDishesCode(dsreplaceObject
					 * .getString("dishesCode"));
					 * dishesSetDishesReplaces.add(dsreplace); }
					 */
					dsDish.setDishesSetDishesReplaces(dishesSetDishesReplaces);
					dsDishes.add(dsDish);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (JsonParseException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			String dishesTasteStr = cursor.getString(cursor
					.getColumnIndex("dishesTasteStr"));
			String pungentLevel = cursor.getString(cursor
					.getColumnIndex("pungentLevel"));
			String isStopSell = cursor.getString(cursor
					.getColumnIndex("isStopSell"));
			String isRulingPrice = cursor.getString(cursor
					.getColumnIndex("isRulingPrice"));
			String rulingPrice = cursor.getString(cursor
					.getColumnIndex("rulingPrice"));
			String dishesStyleStr = cursor.getString(cursor
					.getColumnIndex("dishesStyleStr"));
			String dishesMaterialStr = cursor.getString(cursor
					.getColumnIndex("dishesMaterialStr"));
			String notes = cursor.getString(cursor.getColumnIndex("notes"));
			String estimateStr = cursor.getString(cursor
					.getColumnIndex("estimate"));
			int dishorderNum = cursor.getInt(cursor
					.getColumnIndex("dishorderNum"));

			/*
			 * 下面的其实是给购物车数据表用的
			 * 
			 * String tasteIdArray =
			 * cursor.getString(cursor.getColumnIndex("tasteIdArray")); String
			 * tasteNameArray =
			 * cursor.getString(cursor.getColumnIndex("tasteNameArray")); String
			 * avoidfoodIdArray =
			 * cursor.getString(cursor.getColumnIndex("avoidfoodIdArray"));
			 * String avoidfoodNameArray =
			 * cursor.getString(cursor.getColumnIndex("avoidfoodNameArray"));
			 * String ladu_value =
			 * cursor.getString(cursor.getColumnIndex("ladu_value")); String
			 * notes = cursor.getString(cursor.getColumnIndex("notes"));
			 */

			dish.setDishesId(id);
			dish.setCategoryId(categoryId);
			dish.setPrice(price);
			dish.setDishesName(name);
			dish.setDishesCode(dishescode);
			dish.setSaleNum(salenum);
			dish.setUnitName(unitname);
			dish.setIsSet(isset);
			dish.setIsRecommend(isRecommend);
			dish.setRecommendDesc(recommendDesc);
			dish.setIsSpecialty(isSpecialty);
			dish.setSpecialtyDesc(specialtyDesc);
			dish.setMemberPrice(memberPrice);
			dish.setSpecialPrice(specialPrice);
			// dish.setSpiName(spiName);
			dish.setPicUrls(picUrls);
			dish.setLowpicUrls(lowpicUrls);
			dish.setDsDishes(dsDishes);
			dish.setDishesTasteStr(dishesTasteStr);
			dish.setPungentLevel(pungentLevel);
			dish.setIsStopSell(isStopSell);
			dish.setIsRulingPrice(isRulingPrice);
			// dish.setSpTime(spTime);
			// dish.setSpDateStr(spDateStr);
			dish.setDishesStyleStr(dishesStyleStr);
			dish.setDishesMaterialStr(dishesMaterialStr);
			dish.setRulingPrice(rulingPrice);
			dish.setNotes(notes);
			if (estimateStr != null) {
				float estimate = Float.valueOf(estimateStr);
				dish.setEstimate(estimate);
			}
			dish.setDishorderNum(dishorderNum);

			dish.setSpecialDishes(spiDishes);
			/*
			 * 下面的其实是给购物车数据表用的
			 * 
			 * dish.setTasteIdArray(tasteIdArray);
			 * dish.setTasteNameArray(tasteNameArray);
			 * dish.setAvoidfoodIdArray(avoidfoodIdArray);
			 * dish.setAvoidfoodNameArray(avoidfoodNameArray);
			 * dish.setLadu_value(ladu_value); dish.setNotes(notes);
			 */
			dishes.add(dish);
		}
		return dishes;
	}

	/**
	 * 根据数字编码查询菜肴
	 * 
	 * @param searchCode
	 * @return
	 */
	public List<String> searchBySearchCodes2(String searchCode) {
		List<String> dishesNames = null;
		StringBuffer buff = new StringBuffer();
		buff.append("select * from dish where 1=1");
		if (!"".equals(searchCode)) {
		}
		buff.append(" and dishescode like ");
		buff.append("'%" + searchCode + "%'");
		buff.append(" order by salenum desc");
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery(buff.toString(), null);
		if (cursor.getCount() > 0) {
			dishesNames = new ArrayList<String>();
			while (cursor.moveToNext()) {
				String name = cursor.getString(cursor.getColumnIndex("name"));
				dishesNames.add(name);
			}
		}
		cursor.close();
		dbHelper.closeDatabase();
		return dishesNames;
	}

	/**
	 * 更改菜肴表中某个菜肴的已点数量
	 */
	public void updateNum(String dishId, int dishorderNum) {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("dishorderNum", dishorderNum);
		db.update(TABLE_NAME, values, "id =?", new String[] { dishId });
		dbHelper.closeDatabase();
	}

	/**
	 * 菜肴表已点数量列中在原有基础上加上这些数量（给详细页面用）
	 */
	public void updateAddNum(String dishId, int num) {
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery("select * from dish where id=?",
				new String[] { dishId });
		int dishorderNum = 0;
		while (cursor.moveToNext()) {
			dishorderNum = cursor.getInt(cursor.getColumnIndex("dishorderNum"));
		}
		dishorderNum = dishorderNum + num;
		ContentValues values = new ContentValues();
		values.put("dishorderNum", dishorderNum);
		db.update(TABLE_NAME, values, "id =?", new String[] { dishId });
		cursor.close();
		dbHelper.closeDatabase();
	}

	/**
	 * 菜肴表已点数量列中在原有基础上减去这些数量（给购物车的删除按钮用）
	 */
	public void updateMinusNum(String dishId, float num) {
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery("select * from dish where id=?",
				new String[] { dishId });
		float dishorderNum = 0;
		while (cursor.moveToNext()) {
			dishorderNum = cursor.getInt(cursor.getColumnIndex("dishorderNum"));
		}
		dishorderNum = dishorderNum - num;
		ContentValues values = new ContentValues();
		values.put("dishorderNum", dishorderNum);
		db.update(TABLE_NAME, values, "id =?", new String[] { dishId });
		cursor.close();
		dbHelper.closeDatabase();
	}

	/**
	 * 菜肴表已点数量列全部清空(给购物车的全部清空用)
	 */
	public void clearDishOrderNum() {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("dishorderNum", 0);
		db.update(TABLE_NAME, values, null, null);
		dbHelper.closeDatabase();
	}

	/**
	 * 菜肴表已点数量列自加1个
	 */
	public void addNum(String dishId) {
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery("select * from dish where id=?",
				new String[] { dishId });
		int dishorderNum = 0;
		while (cursor.moveToNext()) {
			dishorderNum = cursor.getInt(cursor.getColumnIndex("dishorderNum"));
		}
		++dishorderNum;
		ContentValues values = new ContentValues();
		values.put("dishorderNum", dishorderNum);
		db.update(TABLE_NAME, values, "id =?", new String[] { dishId });
		cursor.close();
		dbHelper.closeDatabase();
	}

	/**
	 * 菜肴表已点数量列自减1个
	 */
	public void minusNum(String dishId) {
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery("select * from dish where id=?",
				new String[] { dishId });
		int dishorderNum = 0;
		while (cursor.moveToNext()) {
			dishorderNum = cursor.getInt(cursor.getColumnIndex("dishorderNum"));
		}
		--dishorderNum;
		ContentValues values = new ContentValues();
		values.put("dishorderNum", dishorderNum);
		db.update(TABLE_NAME, values, "id =?", new String[] { dishId });
		cursor.close();
		dbHelper.closeDatabase();
	}

	/**
	 * 清空菜肴表中dishorderNum列的值
	 */
	public void cleardishorderNum() {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("dishorderNum", 0);
		db.update(TABLE_NAME, values, null, null);
		dbHelper.closeDatabase();
	}

	/**
	 * 获取一级菜类下的所有的二级菜类ID
	 */
	public List<String> getsubCategoryId(String categoryId) {
		List<String> subCategoryIds = new ArrayList<String>();
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery(
				"select id from dish_category where parentCategoryId=?",
				new String[] { categoryId });
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex("id"));
			subCategoryIds.add(id);
		}
		return subCategoryIds;
	}
}
